package com.liuxinlong.modules.system.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.constant.ThreadLocalConstants;
import com.liuxinlong.enums.ReturnCodes;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.User;
import com.liuxinlong.modules.entity.UserStandardPermission;
import com.liuxinlong.modules.system.controller.request.ImportUserReq;
import com.liuxinlong.modules.system.controller.request.QueryUserOnlineReq;
import com.liuxinlong.modules.system.controller.request.QueryUserReq;
import com.liuxinlong.modules.system.controller.request.UpdateStandardPermissionReq;
import com.liuxinlong.modules.system.controller.request.UpdateUserExpReq;
import com.liuxinlong.modules.system.controller.request.UpdateUserPassReq;
import com.liuxinlong.modules.system.controller.request.UpdateUserReq;
import com.liuxinlong.modules.system.controller.request.UserLoginReq;
import com.liuxinlong.modules.system.controller.response.UserLoginResp;
import com.liuxinlong.modules.system.service.UserService;
import com.liuxinlong.utils.ThreadlocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户信息控制层
 * 
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2021-12-28
 */
@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	@Autowired
	private UploadService uploadService;

	/**
	 * 分页查询用户信息
	 * 
	 * @param req 请求信息
	 * @return 用户信息列表
	 */
	@PostMapping("page")
	@LogAnnotation(value ="分页查询",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_QUERY)
	public BaseResp pageUserInfo(@RequestBody QueryUserReq req) {
		Map<String, Object> queryMap = new HashMap<>();
		queryMap.put("startNum", req.getStartNum());
		queryMap.put("pageSize", req.getPageSize());
		queryMap.put("status", req.getStatus());
		queryMap.put("account", req.getAccount());
		queryMap.put("name", req.getName());
		queryMap.put("department", req.getDepartment());
		queryMap.put("role", req.getRole());
		queryMap.put("startTime", req.getStartTime());
		queryMap.put("endTime", req.getEndTime());
		List<Map<String, Object>> dataList = userService.pageUserInfo(queryMap);
		int totalNum = userService.getUserCount(queryMap);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("分页查询用户信息成功");
		resp.setDataList(dataList);
		resp.setTotalNum(totalNum);
		return resp;
	}

	/**
	 * 新增用户信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("add")
	@LogAnnotation(value ="新增用户",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_ADD)
	public BaseResp addUserInfo(@RequestBody UpdateUserReq req) {
		User user = new User();
		user.setAccount(req.getAccount());
		user.setName(req.getName());
		user.setCardAccount(req.getCardAccount());
		user.setDepartment(req.getDepartment());
		user.setTeam(req.getTeam());
		user.setJoinDate(req.getJoinDate());
		user.setJobType(req.getJobType());
		user.setJobStartTime(req.getJobStartTime());
		user.setJobEndTime(req.getJobEndTime());
		user.setBirthday(req.getBirthday());
		user.setIdNumber(req.getIdNumber());
		user.setNativePlace(req.getNativePlace());
		user.setAddress(req.getAddress());
		user.setRemark(req.getRemark());
		user.setSn(req.getSn());
//		user.setAbbreviationName(req.getAbbreviationName());
		user.setOther(req.getOther());
		userService.addUserInfo(user,req.getRole(),req.getPost());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("新增用户信息成功");
		return resp;

	}

	/**
	 * 修改用户信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("update")
	@LogAnnotation(value ="修改用户",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp updateUserInfo(@RequestBody UpdateUserReq req) {
		User user = new User();
		user.setId(req.getId());
		user.setAccount(req.getAccount());
		user.setName(req.getName());
		user.setCardAccount(req.getCardAccount());
		user.setDepartment(req.getDepartment());
		user.setTeam(req.getTeam());
		user.setJoinDate(req.getJoinDate());
		user.setJobType(req.getJobType());
		user.setJobStartTime(req.getJobStartTime());
		user.setJobEndTime(req.getJobEndTime());
		user.setBirthday(req.getBirthday());
		user.setIdNumber(req.getIdNumber());
		user.setNativePlace(req.getNativePlace());
		user.setAddress(req.getAddress());
		user.setRemark(req.getRemark());
		user.setSn(req.getSn());
//		user.setAbbreviationName(req.getAbbreviationName());
		user.setOther(req.getOther());
		userService.updateUserInfo(user,req.getRole(),req.getPost());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("修改用户信息成功");
		return resp;
	}

	/**
	 * 删除用户信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("delete")
	@LogAnnotation(value ="删除用户",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_DELETE)
	public BaseResp deleteUserInfo(@RequestBody BaseDeleteReq req) {
		userService.deleteUserInfo(req.getId());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("删除用户信息成功");
		return resp;
	}

	/**
	 * 用户登录
	 * 
	 * @param req 用户登录请求信息
	 * @return 用户登录返回信息
	 */
	@PostMapping("login")
	@LogAnnotation(value ="用户登录",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
	public UserLoginResp userLogin(@RequestBody UserLoginReq req) {
		User user = new User();
		user.setAccount(req.getUsername());
		user.setPassword(req.getPassword());
		Map<String, Object> loginResultMap = userService.userLogin(user);
		UserLoginResp resp = new UserLoginResp();
		resp.setCurrentAuthority((String) loginResultMap.get("userName"));
		resp.setAuthority(loginResultMap.get("authority"));
		resp.setButtonAuthority(loginResultMap.get("buttonAuthority"));
		resp.setToken((String) loginResultMap.get("token"));
		resp.setType(req.getType());
		resp.setDefaultPass((String)loginResultMap.get("defaultPass"));
		resp.setRetCode(ReturnCodes.HTTP_SUCCESS.getRetCode());
		resp.setRetMessage("用户登录成功");
		resp.setStatus("ok");
		return resp;
	}

	/**
	 * 用户密码修改
	 *
	 * @param req 用户信息
	 * @return 成功标识信息
	 */
	@PostMapping("password")
	@LogAnnotation(value ="修改密码",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp updatePassword(@RequestBody UpdateUserPassReq req) {
		userService.updatePassword(req.getOldPass(),req.getNewPass());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("用户密码修改成功");
		return resp;
	}

	/**
	 * 用户登出
	 * 
	 * @return 用户登出返回信息
	 */
	@PostMapping("logout")
	@LogAnnotation(value ="用户登出",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp userLogout() {
		userService.userLogout();
		BaseResp resp = new BaseResp();
		resp.setRetMessage("用户登出成功");
		return resp;
	}

	/**
	 * 查询当前登陆用户信息
	 *
	 * @return 用户登录返回信息
	 */
	@GetMapping("current")
//	@LogAnnotation(value ="查询当前用户信息",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_QUERY)
	public BaseResp getCurrentUser(HttpServletRequest request) {
		Map<String, Object> resultMap = userService.getCurrentUser();
		BaseResp resp = new BaseResp();
		resp.setDataMap(resultMap);
		resp.setRetMessage("查询当前登陆用户信息成功");
		return resp;
	}

	/**
	 * 强制下线用户
	 *
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("offline")
	@LogAnnotation(value ="强制下线",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp offlineUser(@RequestBody BaseDeleteReq req) {
		userService.offlineUser(req.getId());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("强制下线用户成功");
		return resp;
	}

	/**
	 * 配置用户黑名单
	 *
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("black")
	@LogAnnotation(value ="黑名单配置",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp blackUser(@RequestBody BaseDeleteReq req) {
		userService.blackUser(req.getId());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("用户黑名单配置成功");
		return resp;
	}

	/**
	 * 重置用户密码
	 *
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("reset")
	@LogAnnotation(value ="重置用户密码",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp resetUser(@RequestBody BaseDeleteReq req) {
		userService.resetUser(req.getId());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("重置用户密码成功");
		return resp;
	}

	/**
	 * 查询用户信息
	 *
	 * @return 成功标识
	 */
	@GetMapping ("base")
	@LogAnnotation(value ="查询用户信息",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_QUERY)
	public BaseResp queryUserBase() {
		User current = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
		Map<String,Object> dataMap = userService.queryUserBase(current.getId());
		BaseResp resp = new BaseResp();
		resp.setDataMap(dataMap);
		resp.setRetMessage("查询用户信息成功");
		return resp;
	}

	/**
	 * 查询用户拓展信息
	 *
	 * @return 成功标识
	 */
	@GetMapping ("info/query")
	@LogAnnotation(value ="查询用户拓展信息",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_QUERY)
	public BaseResp queryUserExpInfo() {
		Map<String,Object> dataMap = userService.queryUserExpInfo();
		BaseResp resp = new BaseResp();
		resp.setDataMap(dataMap);
		resp.setRetMessage("查询用户信息成功");
		return resp;
	}

	/**
	 * 修改用户拓展信息
	 *
	 * @return 成功标识
	 */
	@PostMapping ("info/update")
	@LogAnnotation(value ="修改用户拓展信息",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp updateUserExpInfo(@RequestBody UpdateUserExpReq req) {
		User user = new User();
		user.setId(req.getId());
//		user.setAbbreviationName(req.getAbbreviationName());
		user.setPortrait(req.getPortrait());
//		user.setProfile(req.getProfile());
		user.setAddress(req.getAddress());
		userService.updateUserExpInfo(user);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("修改用户信息成功");
		return resp;
	}

	/**
	 * 查询用户所在用户组
	 *
	 * @return 成功标识
	 */
	@PostMapping ("group")
	@LogAnnotation(value ="查询用户所在组信息",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_QUERY)
	public BaseResp queryUserGroup(@RequestBody BaseDeleteReq req) {
		List<String> dataList = userService.queryUserGroup(req.getId());
		BaseResp resp = new BaseResp();
		Map<String,Object> dataMap = new HashMap<>();
		dataMap.put("userGroup",dataList);
		resp.setDataMap(dataMap);
		resp.setRetMessage("查询用户所在组信息成功");
		return resp;
	}

	/**
	 * 导入数据预处理
	 *
	 * @param multipartRequest 文件
	 * @return 数据
	 */
	@PostMapping("import")
	@LogAnnotation(value ="导入用户数据",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_IMPORT)
	public ImportResp importUser(MultipartRequest multipartRequest) {
		Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
		ImportResp resp = userService.importUser((String) map.get("fileRealPath"));
		return resp;
	}

	/**
	 * 导入用户信息
	 *
	 * @param req 请求体
	 * @return 返回导入失败数据
	 */
	@PostMapping("batch-add")
	@LogAnnotation(value ="导入用户数据",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_IMPORT)
	public ImportResp batchAddUser(@RequestBody ImportUserReq req) {
		return userService.batchAddUser(req.getDataList());
	}

	/**
	 * 导出用户信息
	 *
	 * @return 导出数据
	 */
	@PostMapping("export")
	@LogAnnotation(value ="导出用户信息",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_EXPORT)
	public void exportUser(HttpServletResponse response) {
		userService.exportUser(response);
	}

	/**
	 * 查询用户标准权限
	 *
	 * @return 成功标识
	 */
	@PostMapping ("standard")
	@LogAnnotation(value ="查询用户标准权限",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_QUERY)
	public BaseResp queryStandardPermission(@RequestBody QueryBaseReq req) {
		Map<String,Object> queryParam = new HashMap<>();
		queryParam.put("startNum", 0);
		queryParam.put("pageSize", 100);
		queryParam.put("userId", req.getId());
		List<Map<String,Object>> dataList = userService.queryStandardPermission(queryParam);
		BaseResp resp = new BaseResp();
		resp.setDataList(dataList);
		resp.setRetMessage("查询用户标准权限信息成功");
		return resp;
	}

	/**
	 * 修改用户标准权限
	 *
	 * @return 成功标识
	 */
	@PostMapping ("standard-update")
	@LogAnnotation(value ="修改用户标准权限",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp updateStandardPermission(@RequestBody UpdateStandardPermissionReq req) {
		UserStandardPermission userStandardPermission = new UserStandardPermission();
		userStandardPermission.setId(req.getId());
		userStandardPermission.setStandardId(req.getStandardId());
		userStandardPermission.setUserId(req.getUserId());
		userStandardPermission.setExpireTime(req.getExpireTime());
		userStandardPermission.setAttach(req.getAttach());
		userService.updateStandardPermission(userStandardPermission);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("查询用户标准权限信息成功");
		return resp;
	}

	/**
	 * 修改用户标准权限
	 *
	 * @return 成功标识
	 */
	@PostMapping ("standard-delete")
	@LogAnnotation(value ="删除用户标准权限",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_DELETE)
	public BaseResp deleteStandardPermission(@RequestBody BaseDeleteReq req) {
		userService.deleteStandardPermission(req.getId());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("删除用户标准权限信息成功");
		return resp;
	}

	/**
	 * 同步用户企业微信信息
	 *
	 * @return 成功标识
	 */
	@GetMapping ("wechat-sync")
	@LogAnnotation(value ="同步用户企业微信信息",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp syncWechatInfo() {
		userService.syncWechatInfo();
		BaseResp resp = new BaseResp();
		resp.setRetMessage("同步用户企业微信信息成功");
		return resp;
	}

	/**
	 * 查询用户在线数量统计信息
	 *
	 * @return 成功标识
	 */
	@PostMapping ("online-statistics")
	@LogAnnotation(value ="查询用户在线统计数据",business = LogBusinessConstants.USER_MANAGE,operateType = LogOperateConstants.OPERATE_QUERY)
	public BaseResp queryUserOnlineStatistics(@RequestBody QueryUserOnlineReq req) {
		Map<String,Object> queryParam = new HashMap<>();
		queryParam.put("startTime",req.getStartTime());
		queryParam.put("endTime",req.getEndTime());
		queryParam.put("searchType",req.getSearchType());
		List<Map<String,Object>> dataList = userService.queryUserOnlineStatistics(queryParam);
		BaseResp resp = new BaseResp();
		resp.setDataList(dataList);
		resp.setRetMessage("查询用户在线数量统计信息成功");
		return resp;
	}
}
